package com.yuke.cloud.service.cmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillAndSkuDto;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillSkuDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.cmc.dto.CmsAppTopicDataDetailDto;
import com.yuke.cloud.service.cmc.dto.CmsAppTopicInfoDto;
import com.yuke.cloud.service.cmc.dto.CmsAppTopicSpecInfoDto;
import com.yuke.cloud.service.cmc.entity.CmsAppTopic;
import com.yuke.cloud.service.cmc.service.CmsAppTopicService;
import com.yuke.cloud.service.cmc.service.CmsStorageSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* Created by wg on 2019/01/18.
*/
@Api(value = "WEB - MallCmsStorageSpecController", tags = "处理CmsAppAd相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/mall/cms/storage")
public class MallCmsStorageSpecController extends BaseController {
    @Resource
    private CmsStorageSpecService cmsStorageSpecService;
    @Resource
    private CmsAppTopicService cmsAppTopicService;

    /**
     * 分页获取一个主题下所有商品记录
     * @param cmsAppTopicDataDetailDto
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/app/topicDataDetail")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdSpecInfoDto>> getAppDetailByTopicId(@ApiParam(name = "cmsAppTopic", value = "根据主题名称查找") @RequestBody CmsAppTopicDataDetailDto cmsAppTopicDataDetailDto,
    @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
     @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdSpecInfoDto> list = cmsStorageSpecService.getAppDetailByTopicId(cmsAppTopicDataDetailDto);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 批量获取多个主题下的包含主题信息和商品的全部记录
     * @param cmsAppTopicDataDetailDto
     * @return
     */
    @PostMapping("/app/topicDataDetailList")
    @ApiOperation(httpMethod = "POST", value = "获取所有记录")
    public Wrapper<List<CmsAppTopicDataDetailDto>> getAppDetailByTopicIds(@ApiParam(name = "cmsAppTopic", value = "根据主题名称查找") @RequestBody CmsAppTopicDataDetailDto cmsAppTopicDataDetailDto
                                                                         ) {

        if (cmsAppTopicDataDetailDto!=null){
            if (cmsAppTopicDataDetailDto.getShopId()==null){
                cmsAppTopicDataDetailDto.setShopId(220854931912392704L);
            }
        }
        List<CmsAppTopicDataDetailDto> list = cmsStorageSpecService.getAppDetailByTopicIdList(cmsAppTopicDataDetailDto);

        return WrapMapper.ok(list);
    }

    /**
     * 获取活动商品，秒杀商品的信息
     * @param count
     * @param shopId
     * @return
     */
    @GetMapping("/app/getSecKill")
    @ApiOperation(httpMethod = "GET", value = "获取秒杀活动及商品")
    public Wrapper<List<CmsSecKillAndSkuDto>> getSecKill(@ApiParam(name = "count", value = "需要返回的记录个数", defaultValue = "5") @RequestParam(defaultValue = "5") Integer count,
                                                         @RequestParam(defaultValue = "220854931912392704") Long shopId) {

        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = cmsStorageSpecService.getSecKillAndSpecs(count,shopId);


        return WrapMapper.ok(cmsSecKillAndSkuDtos);
    }

    /**
     * 获取秒杀活动的更多商品
     * @param secKillId
     * @param shopId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getSecKillSkus/{secKillId}")
    @ApiOperation(httpMethod = "GET", value = "获取秒杀活动的更多商品")
    public Wrapper<PageInfo<CmsSecKillSkuDto>> getSecKillSkus(@ApiParam(name = "secKillId", value = "秒杀活动id") @PathVariable Long secKillId,
                                                              @ApiParam(name = "shopId", value = "店铺ID", defaultValue = "0") @RequestParam(defaultValue = "0") Long shopId,
                                                              @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                              @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        if (secKillId != null) {
            List<CmsSecKillSkuDto> list = cmsStorageSpecService.getSecKillSkus(secKillId ,shopId);
            PageInfo pageInfo = new PageInfo(list);
            return WrapMapper.ok(pageInfo);
        } else {
            PageInfo pageInfo = new PageInfo(new ArrayList<>());
            return WrapMapper.ok(pageInfo);
        }
    }


    /**
     * 获取一个主题的信息和主题商品信息
     * @param cmsAppTopicInfoDto
     * @return
     */
    @PostMapping("/app/topicSpecInfoList")
    @ApiOperation(httpMethod = "POST", value = "获取所有记录")
    public Wrapper<CmsAppTopicInfoDto> getAppTopicSpecInfoList(@ApiParam(name = "cmsAppTopic", value = "根据主题名称查找") @RequestBody CmsAppTopicInfoDto cmsAppTopicInfoDto) {
        System.out.println("ACSDDDDDDDDDDDDDDDDDDD");
        if (cmsAppTopicInfoDto == null && cmsAppTopicInfoDto.getTopicId() == null){
            return WrapMapper.error("传入参数错误！");
        }

        if (cmsAppTopicInfoDto!=null){
            if (cmsAppTopicInfoDto.getShopId()==null){
                cmsAppTopicInfoDto.setShopId(220854931912392704L);
            }
        }
        CmsAppTopic cmsAppTopic =  cmsAppTopicService.selectByKey(cmsAppTopicInfoDto.getTopicId());
        if (cmsAppTopic != null){
            BeanUtils.copyProperties(cmsAppTopic,cmsAppTopicInfoDto);
            List<CmsAppTopicSpecInfoDto> list = cmsStorageSpecService.getAppTopicSpecInfoList(cmsAppTopicInfoDto);

            cmsAppTopicInfoDto.setCmsAppTopicSpecInfoDtoList(list);
            return WrapMapper.ok(cmsAppTopicInfoDto);
        }
        return WrapMapper.error("未查找到信息！");
    }
}
